package com.imti.service.impl;

import com.imti.mapper.UserMapper;
import com.imti.model.User;
import com.imti.service.UserService;
import com.imti.util.MD5Util;
import com.imti.util.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户业务实现类
 */
@Service
public class UserServiceImpl implements UserService {

    //注入mapper
    @Resource
    UserMapper userMapper;

    /**
     * 登陆验证
     * @param user
     * @return
     */
    @Override
    public User verifyLogin(User user) {
        String md5 = MD5Util.md5(user.getPassword());
        return userMapper.verifyLogin(user.getUsername(),md5);
    }


    /**
     * 注册
     * @param user
     * @return
     */
    @Override
    public int registed(User user) {
        if ("student".equals(user.getRole())){
            user.setRole("1");//学生：1
        }
        if ("teacher".equals(user.getRole())){
            user.setRole("2");//老师：2
        }
        if ("manager".equals(user.getRole())){
            user.setRole("3");//管理员：3
        }
        String pswd = MD5Util.md5(user.getPassword());
        user.setPassword(pswd);
        return userMapper.registed(user);
    }

    /**
     * 验证登录
     * @param s 用户名
     * @return
     */
    @Override
    public int validname(String s) {
        return userMapper.validname(s);
    }

    /**
     * 查询所有用户信息
     * @param user
     * @return
     */
    @Override
    public List<User> findAllUser(User user, PageBean page) {
//        return userMapper.findAllUser(user,page);
        return null;
    }

    @Override
    public int addUserById(User user) {
        if ("student".equals(user.getRole())){
            user.setRole("1");//学生：1
        }
        if ("teacher".equals(user.getRole())){
            user.setRole("2");//老师：2
        }
        if ("manager".equals(user.getRole())){
            user.setRole("3");//管理员：3
        }
        String pwd = MD5Util.md5(user.getPassword());
        user.setPassword(pwd);
        return userMapper.addUserById(user);
    }

    @Override
    public int vPhone(String p) {
        return userMapper.vPhone(p);
    }

    @Override
    public Integer UpdateUser(User user) {
//        String upwd= MD5Util.md5(user.getPassword());
//        user.setPassword(upwd);
        return userMapper.UpdateUser(user);
    }

    @Override
    public User findUserById(Integer id) {
        return userMapper.findUserById(id);
    }

    @Override
    public boolean resPassword(Integer id) {
        return userMapper.resPassword(MD5Util.md5("123456"),id);
    }

    @Override
    public List<User> selectAllStu() {
        return userMapper.selectAllStu();
    }

    @Override
    public void updateStuClaz(String[] selectedIds, Integer id) {
        for (int i = 0; i < selectedIds.length; i++) {
            userMapper.updateStuClaz(selectedIds[i],id);
        }
    }

    @Override
    public boolean getUserName(String name) {
        return userMapper.getUserName(name)>0?false:true;
    }

    @Override
    public PageBean userPage(User user, PageBean page) {
        if(page.getCurrtPage()==null){
            page.setCurrtPage(1);
        }
        if(page.getPageLimit()==null){
            page.setPageLimit(5);
        }
        //查询总条数
        int totalCount=userMapper.getUserCount(user);
        //查询分页列表
        Map map=new HashMap();
        map.put("u",user);
        map.put("p",page);
        List<User> list=userMapper.findAll(map);
        //封装pagebean
        PageBean pages=new PageBean(totalCount,page.getCurrtPage(),page.getPageLimit(),list,"users/allUsers",getParams(user));
        return pages;
    }

    /**
     * 封装分页搜索查询的参数
     * @param user
     * @return
     */
    public String getParams(User user){
        StringBuilder sb=new StringBuilder();
        if(user.getUsername()!=null && !"".equals(user.getUsername())){
            sb.append("&username=").append(user.getUsername());
        }
        if(user.getPhone()!=null && !"".equals(user.getPhone())){
            sb.append("&phone=").append(user.getPhone());
        }
        if(user.getLockFlag()!=null && !"".equals(user.getLockFlag())){
            sb.append("&lockFlag=").append(user.getLockFlag());
        }
        return sb.toString();
    }

}
